﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIPO.Library.DataAccess.Transaction;
using System.Configuration;
using System.Data;
using Microsoft.Reporting.WebForms;
using SIPO.Web.Support;

public partial class reports_Transaction_rptPrintSanction : System.Web.UI.Page
{
    recSanctionRepo recSanctionDal = new recSanctionRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
    private string parameters = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DateTime? dPeriodeAwal = Context.Request.Params["periodeAwal"].Equals(string.Empty) ? (DateTime?)null : Helper.FormatDate(Context.Request.Params["PeriodeAwal"], Helper.Culture.Indonesian);
            DateTime? dPeriodeAkhir = Context.Request.Params["periodeAkhir"].Equals(string.Empty) ? (DateTime?)null : Helper.FormatDate(Context.Request.Params["PeriodeAkhir"], Helper.Culture.Indonesian);
            string debtorID = Context.Request.Params["debtorID"].Equals(string.Empty) ? string.Empty : Context.Request["debtorID"];
            string sanctionTypeID = Context.Request.Params["sanctionTypeID"].Equals(string.Empty) ? string.Empty : Context.Request.Params["sanctionTypeID"];
            string isReview = Context.Request.Params["isReview"].Equals(string.Empty) ? string.Empty : Context.Request.Params["IsReview"];
            string isApprove = Context.Request["isApprove"].Equals(string.Empty) ? string.Empty : Context.Request["isApprove"];

            //konversi tanggal
            string sPeriodeAwal = string.Format("{0:yyyyMMdd}", dPeriodeAwal);
            string sPeriodeAkhir = string.Format("{0:yyyyMMdd}", dPeriodeAkhir);

            //filter isReview
            if (isReview.Equals("1"))
                parameters += "WHERE recSanction.IsReview = 'false'";
            else if (isReview.Equals("2"))
                parameters += "WHERE recSanction.IsReview = 'true'";

            //filter isApprove
            if (isReview.Equals("3"))
            {
                if (isApprove.Equals("1"))
                    parameters += "WHERE recSanction.IsApprove = 'false'";
                else if (isApprove.Equals("2"))
                    parameters += "WHERE recSanction.IsApprove = 'true'";
            }
            else
            {
                if (isApprove.Equals("1"))
                    parameters += " AND recSanction.IsApprove = 'false'";
                else if (isApprove.Equals("2"))
                    parameters += " AND recSanction.IsApprove = 'true'";
            }

            //filter letterDate
            if (isReview.Equals("3") && isApprove.Equals("3"))
            {
                if (!sPeriodeAwal.Equals(string.Empty) && !sPeriodeAkhir.Equals(string.Empty))
                    parameters += "WHERE recSanction.LetterDate BETWEEN ISNULL ('" + sPeriodeAwal + "', recSanction.LetterDate) AND ISNULL ('" + sPeriodeAkhir + "', recSanction.LetterDate)";
            }
            else
            {
                if (!sPeriodeAwal.Equals(string.Empty) && !sPeriodeAkhir.Equals(string.Empty))
                    parameters += " AND recSanction.LetterDate BETWEEN ISNULL ('" + sPeriodeAwal + "', recSanction.LetterDate) AND ISNULL ('" + sPeriodeAkhir + "', recSanction.LetterDate)";
            }

            //filter debtorID
            if (isReview.Equals("3") && isApprove.Equals("3") && sPeriodeAwal.Equals(string.Empty) && sPeriodeAkhir.Equals(string.Empty))
            {
                if (!debtorID.Equals(string.Empty))
                    parameters += "WHERE recDebtorSector.DebtorID = ISNULL ('" + debtorID + "', recDebtorSector.DebtorID)";
            }
            else if (!debtorID.Equals(string.Empty))
            {
                parameters += " AND recDebtorSector.DebtorID = ISNULL ('" + debtorID + "', recDebtorSector.DebtorID)";
            }

            //filter sanctionTypeID
            if (isReview.Equals("3") && isApprove.Equals("3") && sPeriodeAwal.Equals(string.Empty) && sPeriodeAkhir.Equals(string.Empty) && debtorID.Equals(string.Empty))
            {
                if (!sanctionTypeID.Equals(string.Empty))
                    parameters += "WHERE recSanction.SanctionTypeID = ISNULL ('" + sanctionTypeID + "', recSanction.SanctionTypeID)";
            }
            else if (!sanctionTypeID.Equals(string.Empty))
            {
                parameters += " AND recSanction.SanctionTypeID = ISNULL ('" + sanctionTypeID + "', recSanction.SanctionTypeID)";
            }

            try
            {
                DataTable dt = recSanctionDal.GetList(parameters);

                rsViewer.Reset();
                rsViewer.LocalReport.ReportPath = Server.MapPath("~/reportviewer/Transaction/rptSanction.rdlc");
                ReportDataSource rds = new ReportDataSource();
                rds.Name = "DataSet1";
                rds.Value = dt;

                rsViewer.LocalReport.DataSources.Clear();
                rsViewer.LocalReport.DataSources.Add(rds);
                rsViewer.LocalReport.Refresh();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }
}